package com.hsb.controller;

import com.hsb.entity.Book;
import com.hsb.parsing.ParseByKeyWord;
import com.hsb.utils.ElasticSearchUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;

@Controller
//@ResponseBody
public class ElasticSearchController {
    @ResponseBody
    @RequestMapping("/search/{keyword}")
    public ArrayList<Map<String, Object>> query(Model model, @PathVariable("keyword")String keyword){
        ArrayList<Map<String, Object>> list = ElasticSearchUtil.queryByKeyword(keyword, 0, 8);
        return list;
    }

    @RequestMapping("/")
    public String index(){
        return "index";
    }

    @ResponseBody
    @RequestMapping("/create/index/{name}")
    public Boolean createIndex(@PathVariable("name")String name){
        return ElasticSearchUtil.createIndex(name);
    }

    @ResponseBody
    @RequestMapping("/create/doc/{keyword}")
    public Boolean createDoc(@PathVariable("keyword")String keyword) throws IOException {
        ArrayList<Book> books = ParseByKeyWord.parse(keyword);
        return ElasticSearchUtil.creatDocBatch("book",books);
    }

}
